import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class Consumer implements Runnable {

    private ReentrantLock lock;
    private Condition condition;
    List<String> list;

    public Consumer(ReentrantLock lock, Condition condition, List<String> list) {
        this.lock = lock;
        this.condition = condition;
        this.list = list;
    }

    @Override
    public void run() {
        lock.lock();
        try {
            while (true) {
                if (list.size() == 0) {
                    condition.await();
                }
                if (list.size() == 1) {
                    String string = list.remove(0);
                    System.out.println("consumer: " + string);
                }
                Thread.sleep(2000);
                condition.signal();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

}
